/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var SoundEngine = function(){
    
    if ( SoundEngine.caller != SoundEngine.getInstance ) {  
        throw new Error("This object cannot be instanciated");  
    }
    
    var _assetsPath, _manifest, _preload;
    var _fxEnabled = true;
    
    console.log("Starting loading sounds");
    
    var _init = function(){
        _assetsPath = PATH_AUDIO;
        _manifest = [
            {src:_assetsPath+"sound_ball_colision.mp3", id:"sound_ball_colision", data:1},
            {src:_assetsPath+"sound_ball_desintegration.mp3", id:"sound_ball_desintegration", data:1},
            {src:_assetsPath+"sound_button.mp3", id:"sound_button", data:1},
            {src:_assetsPath+"sound_node_pass.mp3", id:"sound_node_pass", data:1},
            {src:_assetsPath+"sound_node_switch.mp3", id:"sound_node_switch", data:1},
            {src:_assetsPath+"sound_rc_in.mp3", id:"sound_rc_in", data:1},
            {src:_assetsPath+"sound_rc_out.mp3", id:"sound_rc_out", data:1},
            {src:_assetsPath+"sound_rc_wrong.mp3", id:"sound_rc_wrong", data:1},
            {src:_assetsPath+"bg_music.mp3", id:"bg_music", data:1}
        ];
        _preload = new createjs.PreloadJS();
        _preload.installPlugin(createjs.SoundJS);
        _preload.onComplete = function(){
            console.log("SoundEngine should have finished loading");
        }
        _preload.loadManifest(_manifest, true);
    };
    
    _init();
    
    var _playSound = function(target){
        //Play the sound: play (src, interrupt, delay, offset, loop, volume, pan)
        //console.log("SoundEngine : Playing sound '"+target+"'");
        
        if(_fxEnabled){
            createjs.SoundJS.play(target, createjs.SoundJS.INTERRUPT_LATE);
        }
        
       //createjs.SoundJS.play(target);
        
    };
    
    var _playMusic = function(target){
        return createjs.SoundJS.play(target, createjs.SoundJS.INTERRUPT_LATE, 0, 0, -1);
    }
    
    this.ballColision = function(){
        _playSound("sound_ball_colision");
    };
    this.ballDesintegration = function(){
        _playSound("sound_ball_desintegration");
    };
    this.button = function(){
        _playSound("sound_button");
    };
    this.nodePass = function(){
        _playSound("sound_node_pass");
    };
    this.nodeSwitch = function(){
        _playSound("sound_node_switch");
    };
    this.reseachCenterOut = function(){
        _playSound("sound_rc_out");
    };
    this.reseachCenterIn = function(){
        _playSound("sound_rc_in");
    };
    this.reseachCenterWrong = function(){
        _playSound("sound_rc_wrong");
    };
    
    
    var _bgMusic;
    this.startMusic = function(){
        _bgMusic = _playMusic("bg_music");
    }
    
    
    this.toggleMusic = function(){
        if(!_bgMusic){
            this.startMusic();
        }
        _bgMusic.mute(!_bgMusic.muted);
    }
    
    this.toggleFx = function(){
        _fxEnabled = !_fxEnabled;
    }
}

SoundEngine.sharedInstance = null;
SoundEngine.getInstance = function(){
    if(this.sharedInstance == null){
        this.sharedInstance = new SoundEngine();
    }
    return this.sharedInstance;
}